package com.yueqian.epidemic.service.impl;

import com.yueqian.epidemic.beans.DailyEpidemicInfo;
import com.yueqian.epidemic.beans.EpidemicDetailInfo;
import com.yueqian.epidemic.beans.EpidemicInfo;
import com.yueqian.epidemic.beans.ProvinceInfo;
import com.yueqian.epidemic.mapper.EpidemicMapper;
import com.yueqian.epidemic.mapper.ProvinceMapper;
import com.yueqian.epidemic.service.EpidemicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 2020-03-05
 * Loongson
 **/
@Service
public class EpidemicServiceImpl implements EpidemicService {

    @Autowired
    private EpidemicMapper epidemicMapper;

    @Autowired
    private ProvinceMapper provinceMapper;

    @Override
    public List<ProvinceInfo> saveData(DailyEpidemicInfo dailyEpidemicInfo, Integer userId) {
        Date current = new Date();
        String time = dailyEpidemicInfo.getDate();
        String[] timearr = time.split("-");
        short year = 0, month = 0, day = 0;
        if (timearr != null) {
            year = Short.parseShort(timearr[0]);
            month = Short.parseShort(timearr[1]);
            day = Short.parseShort(timearr[2]);
        }

        for (EpidemicInfo epidemic : dailyEpidemicInfo.getArray()) {
            epidemic.setInputDate(current);
            epidemic.setUserId(userId);
            epidemic.setDataYear(year);
            epidemic.setDataMonth(month);
            epidemic.setDataDay(day);
            //保存一条记录
            epidemicMapper.saveInfo(epidemic);
        }
        List<ProvinceInfo> provinces = provinceMapper.findNoDataProvinces(year, month, day);
        return provinces;
    }

    @Override
    public List<EpidemicDetailInfo> findLastestData() {
        //创建一个当前日期对象
        Calendar calendar = new GregorianCalendar();
        short year = (short) calendar.get(Calendar.YEAR);
        short month = (short) (calendar.get(Calendar.MONTH) + 1);
        short day = (short) calendar.get(Calendar.DATE);
        Map<String, Short> condition = new HashMap<>();
        condition.put("year", year);
        condition.put("month", month);
        condition.put("day", day);
        List<EpidemicDetailInfo> detailInfos = epidemicMapper.findLastestData(condition);
        return detailInfos;
    }
}
